package com.hy.z.controller;


import com.hy.z.dto.UserBean;
import com.hy.z.result.Result;
import com.hy.z.service.UserService;
import com.hy.z.utils.JWTUtil;
import com.hy.z.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;


    /**
     * 用户登录
     * 1800
     * 6000
     *
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserBean userParam) {
        UserBean userBean = userService.getUserByUserName(userParam.getUserName(), userParam.getPassword());
        String token = JWTUtil.generatorToken(userParam.getUserName(), 1800000L);
//        Thread.sleep(1500);
        userBean.setToken(token);
        return Result.success(userBean);
    }

    /**
     * 检查token自动登陆 有异常会在JWTUtil抛出
     */
    @RequestMapping("/checkToken")
    public Result index(String token) {
        try {
            JWTUtil.analyzingToken(token);
            return Result.success();
        }catch (Exception e){
            return Result.loginExpire();
        }
    }
}
